--
-- Author: yawei.zhu
-- Date: 2019-01-30 10:58:32
--
-- NewBoxTipsView
--
local IconRender = require("app.widget.renders.IconRender")

local RowSpacing = 10 -- panelContent的行间距、Panel_2/line 
local LineSpacing = 5 -- 每两个文本之间的距离
local MAX_ICON_COUNT = 5
local MAX_WIDTH = 290

local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewBoxTipsNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._renders = {}
	for i = 1, MAX_ICON_COUNT do 
		local icon = self._ui["icon_"..i]
		local render = IconRender.create(icon)
		render:setTipEnable(true)
		render:setTipsCloseCallBack(function()
			self:removeFromParent()
		end)

		self._renders[i] = render
	end

	self._ui.text:getVirtualRenderer():setMaxLineWidth(MAX_WIDTH)
	self._ui.text_1:getVirtualRenderer():setMaxLineWidth(MAX_WIDTH)

	self._ui.Panel:setTouchEnabled(true)
end

function ClassRef:refresh()
	if not self._data then return end

	local name = self._data.name
	local desc = self._data.desc
	local notice = self._data.notice
	local rewards = self._data.rewards

	if not name then name = "" end
	if not desc then desc = "" end
	if not notice then notice = "" end

	self._ui.name:setString(name)
	self._ui.text:setString(desc)
	self._ui.text_1:setString(notice)
	self._ui.line:setVisible(false)

	-- layout icon Panel
	if not rewards or #rewards <= 0 then
		self._ui.icon:setVisible(false)
	else
		self._ui.icon:setVisible(true)
		for i = 1, MAX_ICON_COUNT do
			local render = self._renders[i]
			if rewards[i] then
				render:setVisible(true)
				render:setData(rewards[i])
			else
				render:setVisible(false)
			end
		end
	end


	local height = 0
	if self._ui.icon:isVisible() then
		height = self._ui.icon:getContentSize().height
	end

	if notice ~= "" then
		local size = self._ui.text_1:getContentSize()
		height = height + LineSpacing + size.height 
		self._ui.text_1:setPositionY(height)
	end

	if desc ~= "" then
		self._ui.line:setVisible(true)
		local size = self._ui.line:getContentSize()
		height = height + LineSpacing + size.height 
		self._ui.line:setPositionY(height)

		size = self._ui.text:getContentSize()
		height = height + LineSpacing + size.height 
		self._ui.text:setPositionY(height)
	end

	if name ~= "" then
		local size = self._ui.name:getContentSize()
		height = height + LineSpacing + size.height 
		self._ui.name:setPositionY(height)
	end

	local totalHeight = height + 10 -- 加上 上边距 就是总长
	local panelSize = self._ui.Panel:getContentSize()

	self._ui.Panel:setContentSize(cc.size(panelSize.width, totalHeight))
	self._ui.bg:setContentSize(self._ui.Panel:getContentSize())
	self._ui.bg:setPositionY(totalHeight)
	self:setContentSize(self._ui.Panel:getContentSize())
end


return ClassRef
